jQuery.fn.extend({
    conditionNavRender: function() {
        return this.each(function() {
            $(this).empty(), new jQuery.conditionNav(this);
        });
    }
}), jQuery.conditionNav = function(obj) {
    function setupHidden() {
        var a = document.createElement("input"), b = $(a);
        return b.attr("type", "hidden"), null != $select.attr("name") && b.attr("name", $select.attr("name")), 
        b;
    }
    function createGroup(a, b) {
        var c, d, e, f;
        $select.attr("groupTitleWidth") && (groupTitleWidth = Number($select.attr("groupTitleWidth"))), 
        c = "conditionNavTop", multiMode && (c = "conditionNavTopMulti"), d = '<div class="conditionNavBox"><div class="conditionNavTitle">' + conditionTitle + "</div>" + '<div class="conditionNavContent">' + "</div>" + "</div>", 
        e = b[dataRoot] ? b[dataRoot] : b, $select.append($hidden), e && (a.append(d), f = a.find(".conditionNavContent"), 
        $.each(e, function(b, c) {
            var d, e;
            c.name ? (d = $('<div class="condiSubTitle"><div class="title">' + c.name + '</div><div class="more"></div><div class="clear"></div></div>'), 
            f.append(d)) : alert("数据的分组没有设置名称"), e = $("<ul></ul>"), c.id ? e.attr("id", c.id) : alert("数据的分组没有设置id"), 
            f.append(e), c.height && e.height(c.height), c.maxWidth && (e.hover(function() {
                $(this).width(c.maxWidth), $(this).addClass("groupContent");
            }, function() {
                $(this).width("auto"), $(this).removeClass("groupContent");
            }), d.find(".more").addClass("moreArrow")), e.after('<div class="clear"></div>'), 
            createOptions(e, c[dataRoot], a);
        }), $select.attr("finished", "true"));
    }
    function createOptions(a, b, c) {
        var d, e, f;
        if (b && (d = b[dataRoot] ? b[dataRoot] : b, $.each(d, function(b, d) {
            var e = $('<li class="item"><a></a></li>');
            e.find("a").html(d.key), e.attr("id", d.value), e.attr("parentId", a.attr("id")), 
            a.append(e), multiMode || 0 == b && e.addClass("current2"), e.find("a").click(function() {
                renderItem($(this).parent(), c, 0);
            });
        }), a.append('<div class="clear"></div>'), "-1" != selectedValue)) for (e = selectedValue.split(","), 
        f = 0; f < e.length; f++) a.find("li").each(function() {
            $(this).attr("id") == e[f] && renderItem($(this), c, 1);
        });
    }
    function renderItem(a, b, c) {
        var e, f, g, h, i, d = 1;
        ajaxMode || 0 == c && (d = 0), 1 == d ? (multiMode ? (e = $("<li id=" + a.attr("id") + " parentId=" + a.attr("parentId") + "><a title=" + a.prevAll(".condiSubTitle").text() + a.text() + ">" + a.text() + "</a></li>"), 
        e.find("a").css({
            width: itemWidth + "px",
            height: itemHeight + "px",
            lineHeight: itemHeight + "px"
        }), $("#" + conditionTarget).append(e), a.addClass("current"), a.find("a").unbind("click"), 
        e.click(function() {
            var a, c, d;
            ajaxMode ? (a = $(this), a.remove(), b.find(".item").each(function() {
                $(this).attr("id") == a.attr("id") && (c = $(this));
            }), c && (c.find("a").bind("click", function() {
                renderItem($(this).parent(), b, 0);
            }), c.removeClass("current")), setCurrent()) : "-1" != selectedValue && (d = selectedValue.split(","), 
            removeItemArray(d, $(this).attr("id"), !0), d.length > 0 ? $select.trigger("change", d.join(",")) : $select.trigger("change", "-1"));
        })) : (f = a.attr("parentId"), $("#" + conditionTarget).find("li").each(function() {
            if ($(this).attr("parentId") == f) {
                var a = $(this);
                a.remove(), setCurrent();
            }
        }), e = $("<li id=" + a.attr("id") + " parentId=" + f + "><a title=" + a.prevAll(".condiSubTitle").text() + a.text() + ">" + a.text() + "</a></li>"), 
        e.find("a").css({
            width: itemWidth + "px",
            height: itemHeight + "px",
            lineHeight: itemHeight + "px"
        }), $("#" + conditionTarget).append(e), a.parent().find("li").removeClass("current2"), 
        a.addClass("current2"), e.click(function() {
            if (ajaxMode) {
                var b = $(this);
                b.remove(), a.parent().find("li").removeClass("current2"), a.parent().find("li").eq(0).addClass("current2"), 
                setCurrent();
            } else "-1" != selectedValue && (removeItemArray(selectedValueArr, $(this).attr("id"), !0), 
            selectedValueArr.length > 0 ? $select.trigger("change", selectedValueArr.join(",")) : $select.trigger("change", "-1"));
        })), setCurrent()) : (g = a.attr("id"), "-1" != selectedValue && (multiMode ? g = g + "," + selectedValue : (f = a.attr("parentId"), 
        $("#" + conditionTarget).find("li").each(function() {
            $(this).attr("parentId") == f && (h = $(this).attr("id"), removeItemArray(selectedValueArr, h, !0));
        }), selectedValueArr.length > 0 && (i = selectedValueArr.join(","), g = g + "," + i))), 
        $select.trigger("change", g));
    }
    function setCurrent() {
        var a = "", b = "";
        $("#" + conditionTarget).find("li").each(function() {
            a = a + $(this).attr("id") + ",", b = b + $(this).text() + ",";
        }), a.length > 0 && (a = a.substring(0, a.length - 1)), b.length > 0 && (b = b.substring(0, b.length - 1)), 
        $select.attr("relText", b), $select.attr("relValue", a), $hidden.val(a), $select.trigger("change", a);
    }
    function removeItemArray(a, b, c) {
        var d, e;
        if (c) {
            for (d = [], e = 0; e < a.length; e++) b == a[e] && d.push(a.splice(e, 1)[0]);
            return d;
        }
        for (e = 0; e < a.length; e++) b == a[e] && a.splice(e, 1);
        return a;
    }
    var conditionWidth, groupTitleWidth, $hidden, dataRoot, multiMode, showInfo, showTip, ajaxMode, enabled, selectedValueArr, selectedValue, conditionTitle, conditionTarget, paramsStr, paramsObj, dataObj, urlStr, dataStr, dataObj2, dataType, $select = $(obj), itemWidth = 80, itemHeight = 22, defaultConditionItemWidth = 0, defaultConditionItemHeight = 0;
    if ($select.attr("itemWidth") ? itemWidth = Number($select.attr("itemWidth")) : 0 != defaultConditionItemWidth && (itemWidth = defaultConditionItemWidth), 
    $select.attr("itemHeight") ? itemHeight = Number($select.attr("itemHeight")) : 0 != defaultConditionItemHeight && (itemHeight = defaultConditionItemHeight), 
    conditionWidth = 0, $select.attr("conditionWidth") && (conditionWidth = Number($select.attr("conditionWidth")), 
    $select.width(conditionWidth)), groupTitleWidth = 70, $select.attr("groupTitleWidth") && (groupTitleWidth = Number($select.attr("groupTitleWidth"))), 
    $hidden = setupHidden(), dataRoot = "list", $select.attr("dataRoot") && (dataRoot = $select.attr("dataRoot")), 
    multiMode = !1, ("true" == $select.attr("multiMode") || 1 == $select.attr("multiMode")) && (multiMode = !0), 
    showInfo = !1, ("true" == $select.attr("showInfo") || 1 == $select.attr("showInfo")) && (showInfo = !0), 
    showTip = !1, ("true" == $select.attr("showTip") || 1 == $select.attr("showTip")) && (showTip = !0), 
    ajaxMode = !0, ("false" == $select.attr("ajaxMode") || 0 == $select.attr("ajaxMode")) && (ajaxMode = !1), 
    enabled = !0, $(obj).attr("disabled") && (enabled = "false" == $(obj).attr("disabled") || 0 == $(obj).attr("disabled") ? !0 : !1), 
    selectedValueArr = [], selectedValue = "-1", null != $select.attr("selectedValue") && (selectedValue = $select.attr("selectedValue"), 
    selectedValueArr = selectedValue.split(",")), conditionTitle = "条件筛选", null != $select.attr("conditionTitle") && (conditionTitle = $select.attr("conditionTitle")), 
    conditionTarget = "conditionTarget", null != $select.attr("conditionTarget") && (conditionTarget = $select.attr("conditionTarget")), 
    paramsStr = $select.attr("params")) try {
        paramsObj = JSON.parse(paramsStr);
    } catch (e) {
        paramsObj = [], alert("参数格式不正确！");
    } else paramsObj = [];
    if (dataObj = "", urlStr = $select.attr("url"), dataStr = $select.attr("data"), 
    dataObj2 = $select.data("data")) createGroup($select, dataObj2); else if (dataStr) {
        try {
            dataObj = JSON.parse(dataStr);
        } catch (e) {
            dataObj = "", alert("数据格式不正确！");
        }
        $select.data("data", dataObj), createGroup($select, dataObj);
    } else urlStr && (dataType = "json", $select.attr("dataType") && (dataType = $select.attr("dataType")), 
    $.ajax({
        url: $select.attr("url"),
        dataType: dataType,
        data: paramsObj,
        error: function() {
            alert("url获取数据失败！");
        },
        success: function(data) {
            var myData;
            myData = "text" == dataType ? eval("(" + data + ")") : data, $select.data("data", myData), 
            dataObj = myData, createGroup($select, dataObj);
        }
    }));
};